#include "CPlane.h"
#include "Character/monster/Monster.h"
#include "Character/CreatureMgr.h"
#include "PlaneMgr.h"
#include "Common/TableData/PlanePlaneCfg.h"
#include "Team/GTeamManager.h"
#include "Common/TableDefine.h"
#include "Effect/EffectMgr.h"
#include "Character/monster/MonsterRefCfg.h"
#include "Scene/MapMgr.h"
#include "Common/TableData/AreaAreaCfg.h"
CPlane::CPlane()
{
	m_pConfig = nullptr;
	m_pCreater = nullptr;
}


CPlane::~CPlane()
{
	
}

bool CPlane::Init(uint64_t createrID, uint64_t planeID, uint32_t sceneID,uint32_t teamID)
{
	m_planeID = planeID;
	m_scendID = sceneID;
	m_planeConfigID = 0;
	m_createrID = createrID;
	m_nTeamID = teamID;
	m_vCreatureCid.push_back(createrID);

	if (m_nTeamID>0)
	{
		g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_LEAVE_TEAM, m_nTeamID, CREATURE_PLAYER, "LeaveTeamEvent");
	}

	return true;
}
bool CPlane::UnInit()
{

	if (m_nTeamID>0)
	{
		g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_LEAVE_TEAM, m_nTeamID, CREATURE_PLAYER);
	}

	//销毁时，记得销毁本位面的所有怪物
	for (int i = 0; i < (int)m_vCreatureCid.size(); ++i)
	{
		Creature * pCreature = g_GetCreatureMgr()->GetCreature(m_vCreatureCid[i]);
		if (pCreature)
		{
			if (pCreature->CreatureKind()!=CREATURE_PLAYER && pCreature->CreatureKind()!=CREATURE_NPC)
			{
				pCreature->LeaveScene();
				//直接做销毁
				g_GetCreatureMgr()->Destory(pCreature->GetCid());
				//pCreature->SetDestory();
				
			}
			else
			{
				pCreature->SetPlaneID(0);
			}
		}
	}

	g_GetTimerAxis()->KillAllTimer(this);
	return true;
}

bool CPlane::IsInPlane(uint64_t cid)
{
	if (std::find(m_vCreatureCid.begin(), m_vCreatureCid.end(), cid)!=m_vCreatureCid.end())
	{
		return true;
	}
	return false;
}
bool CPlane::AddInPlane(std::vector<uint64_t> & vCid)
{
	for (int i = 0; i <(int) vCid.size();++i)
	{
		//判断只是不是NPC，则都改其planeid
		Creature * pCreature = g_GetCreatureMgr()->GetCreature(vCid[i]);
		if (!pCreature || pCreature->GetSceneId()!=m_scendID)
		{
			continue;
		}
		if (pCreature->CreatureKind() != CREATURE_NPC )
		{
			if (pCreature->GetPlaneID()>0)
			{
				continue;
			}
			pCreature->SetPlaneID(m_planeID);
		}

		if (pCreature->CreatureKind()==CREATURE_PLAYER)
		{

			if (m_nTeamID>0 && pCreature->GetPAttrValue(P_TEAM_ID) != m_nTeamID)
			{
				LogDebugFmtPrint("add player In Plane error teamID");
				continue;
			}


			PlaneOpenEvent openPlaneEvent;
			openPlaneEvent.createCid = pCreature->GetCid();
			openPlaneEvent.planeConfigID = m_planeConfigID;
			openPlaneEvent.planeID = m_planeID;
			g_GetEvent()->FireExecute(EVENT_PLANE_OPEN, pCreature->GetCid(), CREATURE_PLAYER, (PlaneOpenEvent*)&openPlaneEvent, sizeof(PlaneOpenEvent));


			//发送消息
			OpenPlaneRsp rsp;
			rsp.set_retcode(RET_SUCCESS);
			rsp.set_planeid(m_planeConfigID);
			g_GetLogicService()->SendDataToClient(pCreature->GetCid(), EMODULE_ID_SCENE, LOGIC_TO_CLIENT_PLANE_OPEN_RESUTL, &rsp);
		}

		m_vCreatureCid.push_back(vCid[i]);

		pCreature->UpdateSeeLst();
	}
	return true;
}

void CPlane::RemoveInPlane(std::vector<uint64_t> & vCid)
{
	std::vector<uint64_t>::iterator iter = vCid.begin();
	for (; iter != vCid.end();)
	{
		//判断只是不是NPC，则都改其planeid
		Creature * pCreature = g_GetCreatureMgr()->GetCreature(*iter);
		if (pCreature && pCreature->CreatureKind() != CREATURE_NPC)
		{
			if (pCreature->GetPlaneID()>0)
			{
				pCreature->SetPlaneID(0);
			}
		}
		m_vCreatureCid.erase(iter++);
	}
}

uint32_t CPlane::GetPlaneConfigID()
{
	return m_planeConfigID;
}
void CPlane::SetPlaneConfigID(uint32_t configID)
{
	m_planeConfigID = configID;


	Scene * pScene = g_GetSceneMgr()->GetScene(m_scendID);
	if (pScene)
	{
		m_pCreater = pScene->GetCreature(m_createrID);
		if (m_pCreater)
		{
			//先读配置，看有没有需要刷怪的
			m_pConfig = g_GetPlanePlaneCfgTable()->GetPlanePlaneCfgInfo(m_planeConfigID);
			if (m_pConfig)
			{
				if (m_pConfig->effecttime.length() <= 0)
				{
					//触发所有触发器
					for (int i = 0; i < (int)m_pConfig->vec_effectid.size(); ++i)
					{
						if (m_pConfig->vec_effectid[i]>0)
						{
							g_GetEffectMgr()->OnEffect(m_pCreater, m_pConfig->vec_effectid[i]);
						}
					}
				}
				else
				{
					std::vector<int32_t> vStr;
					CommonApi::SplitStrToVecInt(m_pConfig->effecttime, ";", &vStr);

					for (int i = 0; i < (int)vStr.size(); ++i)
					{
						g_GetTimerAxis()->SetTimer(enTimeID_Effect + i, vStr[i] * 1000, this, 1);
					}
				}

			}
		}
	}

	//刷怪
	if (m_pConfig)
	{
		if (m_pConfig->vecPlanePlaneShuaguaiCfg.size() != 5)
		{
			LogDebugFmtPrint("planecofig refresh monster col error");
			return;
		}

		for (int i = 0; i < (int)m_pConfig->vecPlanePlaneShuaguaiCfg.size(); ++i)
		{
			if (m_pConfig->vecPlanePlaneShuaguaiCfg[i].monster <= 0)
			{
				break;
			}

			//开始定时
			g_GetTimerAxis()->SetTimer(enTimeID_RefreshMonster + i, m_pConfig->vecPlanePlaneShuaguaiCfg[i].time * 1000, this, 1);
		}
	}

}

std::vector<uint64_t>  CPlane::GetNoPlayerList()
{
	std::vector<uint64_t> vCid;
	for (int i = 0; i < (int)m_vCreatureCid.size();++i)
	{
		Creature * pCreature = g_GetCreatureMgr()->GetCreature(m_vCreatureCid[i]);
		if (pCreature && pCreature->CreatureKind()!=CREATURE_PLAYER)
		{
			vCid.push_back(m_vCreatureCid[i]);
		}
	}
	return vCid;
}

std::vector<uint64_t>  CPlane::GetPlayerList()
{
	std::vector<uint64_t> vCid;
	for (int i = 0; i < (int)m_vCreatureCid.size(); ++i)
	{
		Creature * pCreature = g_GetCreatureMgr()->GetCreature(m_vCreatureCid[i]);
		if (pCreature && pCreature->CreatureKind() == CREATURE_PLAYER)
		{
			vCid.push_back(m_vCreatureCid[i]);
		}
	}
	return vCid;

}


uint64_t CPlane::GetCreaterID()
{
	return m_createrID;
}

bool CPlane::LeavePlane(uint64_t cid)
{
	std::vector<uint64_t>::iterator iter = std::find(m_vCreatureCid.begin(), m_vCreatureCid.end(), cid);
	if (iter==m_vCreatureCid.end())
	{
		LogDebugFmtPrint("leave plane error cid is not in plane");
		return false;
	}

	//Creature * pCreature = g_GetCreatureMgr()->GetCreature(cid);
	//if (pCreature && pCreature->CreatureKind()==CREATURE_PLAYER)
	//{
	//	pCreature->SetPlaneID(0);
	//}

	m_vCreatureCid.erase(iter);

	return true;
}

void CPlane::OnExecute(uint16_t nEventID, uint64_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen)
{
	if (EVENT_LEAVE_TEAM == nEventID)
	{
		LeaveTeamEvent * pEvent = static_cast<LeaveTeamEvent *>(pContext);
		if (pEvent)
		{
			g_GetPlaneMgr()->LeavePlane(pEvent->cid);
		}
	}
}

void CPlane::OnTimer(uint32_t nTimerID)
{ 
	if (!m_pCreater || !m_pConfig )
	{
		return;
	}
	if (nTimerID<enTimeID_Effect) //刷怪的
	{
		PlanePlaneShuaguaiCfgInfo pConfig = m_pConfig->vecPlanePlaneShuaguaiCfg[nTimerID - enTimeID_RefreshMonster];

		//先取出该配置,根据类型来创建怪物
		MonsterExtendInfo stExtendInfo;
		//if (pConfig.monster > 0)
		//{
		//	pExtendInfo->set_mastercid(pCreature->GetCid());
		//}

		if (pConfig.lv ==1)
		{
			stExtendInfo.set_level(m_pCreater->GetAttr(C_LEVEL));
		}
		else if (pConfig.lv == 2) // 场景最高等级
		{
			stExtendInfo.set_level(1);  //这个要场景中配置
			Map* pMap=g_GetMapMgr()->GetMap(m_scendID);
			if (pMap)
			{
				stExtendInfo.set_level(pMap->GetOpenLv());
			}
		}
		else if (pConfig.lv == 3) //跟随队伍
		{
			uint32_t level = g_GetGTeamMgr()->GetNearTeamAverageLevel(m_pCreater);
			stExtendInfo.set_level(level);
		}

		uint32_t sceneid = m_pCreater->GetSceneId();
		const AreaAreaCfgInfo * pArea = g_GetAreaAreaCfgTable()->GetAreaAreaCfgInfo(pConfig.area);
		if (pArea)
		{
			sceneid = pArea->belongToSceneID;
		}

		for (int i = 0; i < pConfig.count; ++i)
		{
			Point3<float> tmpPos;
			tmpPos = g_GetMapLocationCfg()->RandPosInArea(pConfig.area);
			Point3<float> curDir = Point3<float>(0, 0, 1);
			//创建怪物
			MonsCreateParam createParam;
			createParam.createType = EMonsCreateType_Plane;
			createParam.createTypeVal = m_pConfig->id;
			g_GetCreatureMgr()->CreateMonster(pConfig.monster, sceneid, tmpPos, createParam, curDir, -1, &stExtendInfo, m_pCreater->GetCid());
		}

		LogDebugFmtPrint("plane create monster boshu=%d", nTimerID - enTimeID_RefreshMonster+1);
	}
	else  //触发器的
	{
		
		g_GetEffectMgr()->OnEffect(m_pCreater, m_pConfig->vec_effectid[nTimerID - enTimeID_Effect]);
		LogDebugFmtPrint("plane oneffect id=%d,effctid=%d", nTimerID - enTimeID_Effect+1, m_pConfig->vec_effectid[nTimerID - enTimeID_Effect]);
	}
}